home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / util / smoothzoom.c < prev   
C/C++ Source or Header  |  1994-08-01  |  5KB  |  165 lines

  1. /*smoothzoom.c
  2.   Eric Pepke
  3.   Does a smooth zoom from one eye position and focus distance to another
  4.  
  5.   Usage:
  6.         smoothzoom zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel
  7.  
  8.   Example:
  9.     smoothzoom 6.0  0 0 -6  2.0 0 1 -2  90 10
  10.  
  11.   starts off with the eye at (0 0 -6) focusing on a point 6 units away and
  12.   zooms to the eye at (0 1 -2) focusing on a point 2 units away.  It takes
  13.   90 frames (3 seconds) to do this and has an acceleration and a deceleration
  14.   step of 10 frames apiece.
  15.  
  16.   The zoom distance is the distance from the eye to the focus point, and it
  17.   is changed via the perspective control.  The eye position is changed by the
  18.   perspective control and by moving the space.  The best way to get the 
  19.   numbers is to save to a log while interactively moving.  There will be a
  20.   begin snapshot/end snapshot block, and a value for LOCATION will be in the
  21.   block.
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <math.h>
  26.  
  27. double zoom1, posn1[3];
  28. double zoom2, posn2[3];
  29.  
  30. void DoFunction(amount, lastAmount)
  31. double amount, lastAmount;
  32. /*Does the function to get to amount (in [0,1]) from lastAmount*/
  33. {
  34.     {
  35.     printf("set value Perspective\\ Control [%lg 25 0.1 8]\n",
  36.         zoom2 * amount + zoom1 * (1.0 - amount));
  37.  
  38.     printf("eyeposn [%lg %lg %lg]\n",
  39.         posn2[0] * amount + posn1[0] * (1.0 - amount),
  40.         posn2[1] * amount + posn1[1] * (1.0 - amount),
  41.         posn2[2] * amount + posn1[2] * (1.0 - amount)
  42.         );
  43.     printf("snap\n");
  44.     }
  45. }
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char *argv[];
  50. {
  51.     double minorStep, amount, lastAmount, check;
  52.     long nSteps, nAccel, accelTotal, nInertial, inertialTotal, totalMinorSteps;
  53.     long progress;
  54.     long k;
  55.     if (argc != 11)
  56.     {
  57.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  58.     exit(-1);
  59.     }
  60.     if (1 != sscanf(argv[1], "%lg", &zoom1))
  61.     {
  62.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  63.     exit(-1);
  64.     }
  65.     if (1 != sscanf(argv[2], "%lg", &(posn1[0])))
  66.     {
  67.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  68.     exit(-1);
  69.     }
  70.     if (1 != sscanf(argv[3], "%lg", &(posn1[1])))
  71.     {
  72.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  73.     exit(-1);
  74.     }
  75.     if (1 != sscanf(argv[4], "%lg", &(posn1[2])))
  76.     {
  77.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  78.     exit(-1);
  79.     }
  80.  
  81.     if (1 != sscanf(argv[5], "%lg", &zoom2))
  82.     {
  83.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  84.     exit(-1);
  85.     }
  86.     if (1 != sscanf(argv[6], "%lg", &(posn2[0])))
  87.     {
  88.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  89.     exit(-1);
  90.     }
  91.     if (1 != sscanf(argv[7], "%lg", &(posn2[1])))
  92.     {
  93.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  94.     exit(-1);
  95.     }
  96.     if (1 != sscanf(argv[8], "%lg", &(posn2[2])))
  97.     {
  98.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  99.     exit(-1);
  100.     }
  101.  
  102.     if (1 != sscanf(argv[9], "%d", &nSteps))
  103.     {
  104.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  105.     exit(-1);
  106.     }
  107.     if (1 != sscanf(argv[10], "%d", &nAccel))
  108.     {
  109.         fprintf(stderr, "usage: %s zoom1 x1 y1 z1 zoom2 x2 y2 z2 nSteps nAccel\n", argv[0]);
  110.     exit(-1);
  111.     }
  112.  
  113.     /*Calculate the number of time steps in the inertial phase*/
  114.     nInertial = nSteps - 2 * nAccel;
  115.     if (nInertial < 0)
  116.     {
  117.     fprintf(stderr, "The total number of steps is not be enough for acceleration.\n");
  118.     exit(-1);
  119.     }
  120.  
  121.     /*Calculate the number of minor steps in each acceleration phase*/
  122.     accelTotal = 0;
  123.     for (k = 1; k <= nAccel; ++k)
  124.     {
  125.     accelTotal += k;
  126.     }
  127.  
  128.     /*Calculate the number of minor steps in the inertial phase*/
  129.     inertialTotal = nInertial * (nAccel + 1);
  130.  
  131.     /*Calculate the total number of minor steps*/
  132.     totalMinorSteps = inertialTotal + 2 * accelTotal;
  133.  
  134.     /*Start off with none done*/
  135.     progress = 0;
  136.     lastAmount = 0;
  137.  
  138.     /*Do acceleration phase*/
  139.     for (k = 1; k <= nAccel; ++k)
  140.     {
  141.     progress += k;
  142.     lastAmount = amount;
  143.     amount = ((double) progress) / ((double) totalMinorSteps);
  144.     DoFunction(amount, lastAmount);
  145.     }
  146.  
  147.     /*Do inertial phase*/
  148.     for (k = 0; k < nInertial; ++k)
  149.     {
  150.     progress += nAccel + 1; 
  151.     lastAmount = amount;
  152.     amount = ((double) progress) / ((double) totalMinorSteps);
  153.     DoFunction(amount, lastAmount);
  154.     }
  155.  
  156.     /*Do deceleration phase*/
  157.     for (k = nAccel; k >= 1; --k)
  158.     {
  159.     progress += k;
  160.     lastAmount = amount;
  161.     amount = ((double) progress) / ((double) totalMinorSteps);
  162.     DoFunction(amount, lastAmount);
  163.     }
  164. }
  165.